package cn.tkb.order.service.impl;

import cn.tkb.basic.service.impl.BaseServiceImpl;
import cn.tkb.basic.util.CodeGenerateUtils;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.order.domain.AdoptOrder;
import cn.tkb.order.domain.OrderAddress;
import cn.tkb.order.mapper.AdoptOrderMapper;
import cn.tkb.order.mapper.OrderAddressMapper;
import cn.tkb.order.query.AdoptOrderQuery;
import cn.tkb.order.service.IAdoptOrderService;
import cn.tkb.org.domain.Employee;
import cn.tkb.org.mapper.EmployeeMapper;
import cn.tkb.pet.domain.Pet;
import cn.tkb.pet.mapper.PetMapper;
import cn.tkb.user.domain.LoginInfo;
import cn.tkb.user.domain.User;
import cn.tkb.user.domain.UserAddress;
import cn.tkb.user.mapper.UserAddressMapper;
import cn.tkb.user.mapper.UserMapper;
import io.netty.util.internal.UnstableApi;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Value("${order.time.out}")//SPEL把application.yml中定义的timeOut的值引用过来
    private Long timeOut;
    /**
     * 生成领养订单实现
     * @param params
     * @param currentLoginInfo
     * @return
     */
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    @Transactional
    public String adopt(Map<String, Object> params, LoginInfo currentLoginInfo) {
        //接收前端传递过来的参数
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());
        Long addressId = Long.valueOf(params.get("address_id").toString());
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString());//1 支付宝 2 微信 3 银联 0其它
        Long petId = Long.valueOf(params.get("pet_id").toString());
        //下架宠物
        User user = userMapper.loadByLoginInfoId(currentLoginInfo.getId());
        //通过宠物id查找该宠物
        Pet pet = petMapper.loadById(petId);
        //1.修改宠物状态为下架
        pet.setState(0);
        //2.绑定领养人
        pet.setUser_id(user.getId());
        //3.保存修改之后的宠物信息
        petMapper.update(pet);
        //生成领养订单
        AdoptOrder order=initAdoptOrder(user,pet);
        //保存领养订单
        adoptOrderMapper.save(order);//返回自增长id
        //保存订单地址
        //1.通过用户的地址id查找到用户的地址
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        //2.根据用户的地址复制一份给订单地址
        OrderAddress orderAddress = initOrderAdress(order, userAddress);
        orderAddressMapper.save(orderAddress);
        return null;
    }

    /**
     * 用户查看领养订单实现
     * @param query
     * @param currentLoginInfo
     * @return
     */
    @Override
    public PageQuery<AdoptOrder> user(AdoptOrderQuery query, LoginInfo currentLoginInfo) {
        User user = userMapper.loadByLoginInfoId(currentLoginInfo.getId());
        query.setUserId(user.getId());
        return super.findPage(query);
    }

    /**
     * 平台或者管理员查看领养订单
     * @param query
     * @param currentLoginInfo
     * @return
     */
    @Override
    public PageQuery<AdoptOrder> admin(AdoptOrderQuery query, LoginInfo currentLoginInfo) {
        Employee employee = employeeMapper.loadByLoginInfoId(currentLoginInfo.getId());
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.findPage(query);
    }

    private OrderAddress initOrderAdress(AdoptOrder order, UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        //给订单地址设置订单的id
        orderAddress.setOrder_id(order.getId());
        //给订单地址设置订单编码
        orderAddress.setOrderSn(order.getOrderSn());
        return orderAddress;
    }

    private AdoptOrder initAdoptOrder(User user, Pet pet) {
        AdoptOrder order = new AdoptOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        //设置成本价
        order.setPrice(pet.getCostprice());
        //设置订单唯一标识
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(user.getId());
        order.setShop_id(pet.getShop_id());
        order.setLastPayTime(new Date(System.currentTimeMillis() + timeOut));
        return order;
    }
}
